<?php

/*
class Checker, checks for invalid values
Copyright (C) 2011  Petr Čermák

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

class CheckerAdapterOff{

    public function checkNull($object){}
    public function checkString($string){}
    public function checkArray($array){}
    public function checkBool($bool){}
    public function checkInteger($integer){}
    public function checkFile($path){}

}

class CheckerAdapterOn{

    public function checkNull($object){
        if($object == null)
            throw new InvalidArgumentException("invalid input: $object");
    }

    public function checkString($string){
        if(!is_string($string) || trim($string) == "")
            throw new InvalidArgumentException("invalid input: $string");
    }

    public function checkArray($array){
        if(!is_array($array))
            throw new InvalidArgumentException("input is not array: $array");
    }

    public function checkBool($bool){
        if(!is_bool($bool))
            throw new InvalidArgumentException("input is not bool: $bool");
    }

    public function checkInteger($integer){
        if(!is_integer($integer))
            throw new InvalidArgumentException("input is not integer: $integer");
    }

    public function checkFile($path){
        $this->checkString($path);
        if(!file_exists($path))
            throw new InvalidArgumentException("file is does not exist: $path");
    }

}

class Checker{

    private static $adapter;
    private static $adapterOn;
    private static $adapterOff;
    private static $initialized = false;

    /**Initializes static variables.*/
    public static function _staticInitializer(){
        if(!self::$initialized){
            self::$adapterOn = new CheckerAdapterOn();
            self::$adapterOff = new CheckerAdapterOff();
            self::$adapter = self::$adapterOn;
            self::$initialized = true;
        }
    }

    /**
     * Checks if Checker is enabled.
     * @return bool
     */
    public static function isEnabled(){
        return self::$adapter == self::$adapterOn;
    }

    /**
     * Enables or disables Checker.
     * @param bool value
     */
    public static function setEnabled($enabled){
        if($enabled == true)self::$adapter = self::$adapterOn;
        else self::$adapter = self::$adapterOff;
    }

    /**
     * Checks if provided object is null, in which case throws an exception.
     * @param Object object to be checked
     */
    public static function checkNull($object){self::$adapter->checkNull($object);}

    /**
     * Checks if string is valid, if not throws an exception.
     * @param string string to be checked
     */
    public static function checkString($string){self::$adapter->checkString($string);}

    /**
     * Checks if provided value is array, if not throws an exception.
     * @param array array to be checked
     */
    public static function checkArray($array){self::$adapter->checkArray($array);}

    /**
     * Checks if provided value is bool, if not throws an exception.
     * @param bool bool to be checked
     */
    public static function checkBool($bool){self::$adapter->checkBool($bool);}

    /**
     * Checks if provided value is integer, if not throws an exception.
     * @param integer integer to be checked
     */
    public static function checkInteger($integer){self::$adapter->checkInteger($integer);}

     /**
     * Checks if provided file path is valid, if not throws an exception.
     * @param string file path to be checked
     */
    public static function checkFile($path){self::$adapter->checkFile($path);}

}

Checker::_staticInitializer();
?>
